package com.gdufe.firesafe.controller;

import com.gdufe.firesafe.annotation.NeedAdmin;
import com.gdufe.firesafe.model.entity.OwnerEntity;
import com.gdufe.firesafe.service.OwnerService;
import com.gdufe.firesafe.utils.PageUtils;
import com.gdufe.firesafe.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 业主表
 *
 * @author Liu
 * @email
 * @date 2024-03-26 16:38:32
 */
@RestController
@RequestMapping("owner/owner")
public class OwnerController {
    @Autowired
    private OwnerService ownerService;

    /**
     * 根据手机号查找
     */
    @RequestMapping("/selectByPhone/{phone}")
    public R selectByPhone(@PathVariable("phone") String phone){
        return R.ok().put("data", ownerService.selectByPhone(phone));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = ownerService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		OwnerEntity owner = ownerService.getById(id);
        if(owner==null){
            return R.error("没有该业主");
        }
        return R.ok().put("owner", owner);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OwnerEntity owner){
		ownerService.save(owner);
        return R.ok().put("data",owner.getId());
    }

    /**
     * 修改
     */
    @NeedAdmin
    @RequestMapping("/update")
    public R update(@RequestBody OwnerEntity owner){
		ownerService.updateById(owner);

        return R.ok();
    }

    /**
     * 删除
     */
    @NeedAdmin
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		ownerService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
